package com.ruiyi.adrobot.mongo.wifi.impl;

import com.ruiyi.adrobot.beans.Model;
import com.ruiyi.adrobot.beans.Wifi;
import com.ruiyi.adrobot.beans.common.MongoPageRequest;
import com.ruiyi.adrobot.mongo.wifi.WifiOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tao.zeng on 2017/5/27.
 */
public class WifiRepositoryImpl implements WifiOperations {
    @Autowired
    private MongoOperations mongo;

    @Override
    public Page<Wifi> getListByPage(MongoPageRequest<Wifi> mongoPageRequest) {
        Wifi params = mongoPageRequest.getParams();
        Query query = new Query();
        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Sort.Order(Sort.Direction.DESC, "id"));
        Sort sort = new Sort(orders);
        mongoPageRequest.setSort(sort);
        Long count = mongo.count(query, Model.class);
        List<Wifi> models = mongo.find(query.with(mongoPageRequest), Wifi.class);
        return new PageImpl<Wifi>(models, mongoPageRequest, count);
    }
}
